// IM初始化
 window.JIM = new JMessage({
         // debug : true
     });
 	$.ajax({
 	url:'http://liuqingwushui.top/api/imchat.php',
 	data:{},
 	success:function(data){
 	JIM.init({
 		"appkey" : data.appkey,
 		"random_str" : data.random_str,
 		"signature" : data.signature,
 		"timestamp" : data.timestamp,
 		"flag" : 0
 	}).onSuccess(function(data) {
 		console.log('success:' + JSON.stringify(data));
		ready();
 	}).onFail(function(data) {
 		console.log('error:' + JSON.stringify(data))
 		 });
 		},
 		error:function(data){
 			mui.alert("鉴权失败");
 		}
 			});

function ready(){
	var user= localStorage.getItem("account");
	var pass= localStorage.getItem("pass");
        JIM.login({
            'username' : user,
			'password':  pass
        }).onSuccess(function(data) {
            console.log(data);
			localStorage.setItem("account", data.username);
			//获取用户信息
			 JIM.getUserInfo({
			            'username' : user,
			        }).onSuccess(function(data) {
			           console.log(data);
					   $("#name").html(data.user_info.nickname);
					   $("#user").html(data.user_info.username);
					   if(data.user_info.avatar!=''){
						    JIM.getResource({
						                    'media_id' : data.user_info.avatar,
						                  }).onSuccess(function(data) {
						                      $("#litpic").attr("src",data.url);
						                  });
					   }
			          }).onFail(function(data) {
			            console.log(data);
			        });
			$("input[type='file']").change(function(){
				var file=this.files[0];
				if(!file.length){
				var fd = new FormData();
				fd.append("avatar",file);
				 JIM.updateSelfAvatar({
				                'avatar' : fd
				               }).onSuccess(function(data) {
								   mui.alert("头像修改成功");
				               }).onFail(function(data) {
								    console.log("头像修改失败");
				               });
				
				}
			})
			
        }).onFail(function(data) {
             mui.openWindow({url:'login.html'})
        }).onTimeout(function(data) {
            mui.openWindow({url:'login.html'})
        });
		}